import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { SongInfo } from '@/types'
import { songAPI, lyricAPI } from '@/api'

export const usePlayerStore = defineStore('player', () => {
  // 状态
  const currentSong = ref<SongInfo | null>(null)
  const playlist = ref<SongInfo[]>([])
  const playing = ref(false)
  const currentTime = ref(0)
  const duration = ref(0)
  const volume = ref(0.7)
  const mode = ref<'sequence' | 'loop' | 'random'>('sequence')
  const lyric = ref('')
  const currentLyricIndex = ref(-1)

  // 计算属性
  const progress = computed(() => {
    if (!duration.value) return 0
    return (currentTime.value / duration.value) * 100
  })

  // 方法
  const play = async (song: SongInfo) => {
    try {
      // 获取歌曲URL
      const urlRes = await songAPI.getSongUrl(song.cid)
      song.url = urlRes.data
      
      // 获取歌词
      const lyricRes = await lyricAPI.getLyric(song.cid)
      lyric.value = lyricRes.data

      currentSong.value = song
      playing.value = true
    } catch (err) {
      console.error('Failed to play song:', err)
    }
  }

  const togglePlay = () => {
    playing.value = !playing.value
  }

  const next = () => {
    if (!playlist.value.length) return
    const currentIndex = playlist.value.findIndex(song => song.id === currentSong.value?.id)
    let nextIndex = 0

    switch (mode.value) {
      case 'sequence':
        nextIndex = (currentIndex + 1) % playlist.value.length
        break
      case 'loop':
        nextIndex = currentIndex
        break
      case 'random':
        nextIndex = Math.floor(Math.random() * playlist.value.length)
        break
    }

    play(playlist.value[nextIndex])
  }

  const prev = () => {
    if (!playlist.value.length) return
    const currentIndex = playlist.value.findIndex(song => song.id === currentSong.value?.id)
    let prevIndex = playlist.value.length - 1

    if (currentIndex > 0) {
      prevIndex = currentIndex - 1
    }

    play(playlist.value[prevIndex])
  }

  const setProgress = (value: number) => {
    currentTime.value = (value / 100) * duration.value
  }

  const setVolume = (value: number) => {
    volume.value = value
  }

  const toggleMode = () => {
    const modes: ('sequence' | 'loop' | 'random')[] = ['sequence', 'loop', 'random']
    const currentIndex = modes.indexOf(mode.value)
    mode.value = modes[(currentIndex + 1) % modes.length]
  }

  const addToPlaylist = (song: SongInfo) => {
    if (!playlist.value.find(s => s.id === song.id)) {
      playlist.value.push(song)
    }
  }

  const removeFromPlaylist = (songId: string) => {
    const index = playlist.value.findIndex(song => song.id === songId)
    if (index > -1) {
      playlist.value.splice(index, 1)
    }
  }

  const clearPlaylist = () => {
    playlist.value = []
    currentSong.value = null
    playing.value = false
  }

  return {
    currentSong,
    playlist,
    playing,
    currentTime,
    duration,
    volume,
    mode,
    lyric,
    currentLyricIndex,
    progress,
    play,
    togglePlay,
    next,
    prev,
    setProgress,
    setVolume,
    toggleMode,
    addToPlaylist,
    removeFromPlaylist,
    clearPlaylist
  }
}) 